<?php
/**
 * Created by JetBrains PhpStorm.
 * User: scala
 * Date: 12-10-24
 * Time: 下午9:27
 * To change this template use File | Settings | File Templates.
 */
class UserAction extends CommonAction
{

    function  index(){
        $user=M("User");
        import("ORG.Util.Page");// 导入分页类
        $count=$user->count();
        $Page= new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfigArray(array('header'=>'位用户','prev'=>'<input type="button" value="<<">','next'=>'<input type="button" value=">>">','first'=>'首页','last'=>'尾页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页   %first% %prePage% %upPage%   <span style="color: red;background-color: #7fffd4;">%linkPage%</span>   %downPage% %nextPage% %end%'));
        $show=$Page->show();//分页显示输出
        $userList=$user->limit($Page->firstRow.",".$Page->listRows)->select();
        $this->assign("userList",$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }


    function update(){
        $user=D("User");
        if($user->create()){
            $user->save();
        }else{
            $this->error($user->getError());
        }
        $this->redirect("index");
    }


    function edit(){
        $user=M("User");
        if(!empty($_GET['id'])){
            $list=$user->find($_GET['id']);
            $this->assign("list",$list);
        }
        $this->display();
    }

    function add(){
        $this->display();
    }


    function insert(){
        $user=D("User");
        if($user->create()){
            if($result=$user->add()){
                $this->redirect("index");
            }else{
                $this->error($user->getError());
            }
        }else{
            $this->error($user->getError());
        }
    }

    function repassword(){
        $this->display();
    }


    function repasswordDo(){
        if($_SESSION [C('USER_AUTH_KEY')]){
            if(empty($_POST['oldpassword'])||empty($_POST['password'])||empty($_POST['repassword'])){
                $this->ajaxReturn("","请填写完整后再更改密码！",0);
            }
            $user=M("User");
            if($list=$user->find($_SESSION [C('USER_AUTH_KEY')])){
                if($list['password']==md5($_POST['oldpassword'])){
                    if($_POST['password']==$_POST['repassword']){
                        $list['password']=md5($_POST['password']);
                        $user->data($list)->save();
                        $this->ajaxReturn("Public/logout","修改密码成功,请重新登录!",2);
                    }else{
                        $this->ajaxReturn("","两次密码不同！",0);
                    }
                }else{
                    $this->ajaxReturn("","您输入的密码与原密码不符合!",0);
                }
            }else{
                $this->ajaxReturn("","系统用户不存在!",0);
            }
        }
        $this->ajaxReturn("","您无权修改密码,没有登录!",0);
    }

    function delete(){
        $user=D("User");
        if(isset($_GET)){
            $id=$_GET["id"];
            if($result=$user->where("id=".$id)->delete()){
                $this->success("删除成功！");
            }else{
                $this->error("删除失败!".$user->getError());
            }
        }else{
            $this->error("没有要删除的系统用户!");
        }
    }

    function upload(){
        if(empty($_FILES)){
            $this->ajaxReturn("ggggggggg","没有选择要上传的文件！",1);
        }
        import("ORG.Net.UploadFile");
        $savepath='./Public/Uploads/faceimg/'.$_POST["id"]."/";
        if(!file_exists($savepath)){
            mkdir($savepath);
        }
        $fileUpload=new UploadFile();
        $fileUpload->maxSize=1000000;
        $fileUpload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $fileUpload->allowTypes=array("image/jpg","image/gif","image/png","image/jpeg");// 上传文件的类型
        $fileUpload->savePath =  $savepath;// 设置附件上传目录
        $fileUpload->uploadReplace=true;//是否覆盖，默认的false
        $fileUpload->saveRule="uniqid";
        $fileUpload->thumb=true;//对图片进行缩略处理
        //设置需要生成缩略图的文件后缀
        $fileUpload->thumbPrefix        = 'm_,s_';  //生产2张缩略图

        //设置缩略图最大宽度
        $fileUpload->thumbMaxWidth      = '400,100';
        //设置缩略图最大高度
        $fileUpload->thumbMaxHeight     = '400,100';

        $fileUpload->thumbRemoveOrigin=true;
//        $fileUpload->autoSub=true;
        if(!$fileUpload->upload()) {// 上传错误提示错误信息
            $this->error($fileUpload->getErrorMsg());
        }else{// 上传成功 获取上传文件信息
            //取得成功上传的文件信息
            $uploadList = $fileUpload->getUploadFileInfo();
            import('ORG.Util.Image');
            Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], './Public/Images/logo.gif',null,10);
            $this->ajaxReturn($uploadList[0]["savename"],"上传成功!",0);
        }

    }

}
